<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.jpg">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"manual","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="1.1 简介 JUC就是java.util.concurrent工具包的简称。这是一个处理线程的工具包，JDK1.5之后有的。  1.2 进程和线程 进程：指在系统中正在运行的一个应用程序；资源分配的最小单位。线程：系统分配处理器时间资源的基本单元，或者说进程之内独立执行的一个单元执行刘。程序执行的最小单位。  1.3 线程的状态1.3.1 线程状态枚举类123456789101112131415">
<meta property="og:type" content="article">
<meta property="og:title" content="JUC概述">
<meta property="og:url" content="http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/index.html">
<meta property="og:site_name" content="陈树立的博客">
<meta property="og:description" content="1.1 简介 JUC就是java.util.concurrent工具包的简称。这是一个处理线程的工具包，JDK1.5之后有的。  1.2 进程和线程 进程：指在系统中正在运行的一个应用程序；资源分配的最小单位。线程：系统分配处理器时间资源的基本单元，或者说进程之内独立执行的一个单元执行刘。程序执行的最小单位。  1.3 线程的状态1.3.1 线程状态枚举类123456789101112131415">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/%E7%94%A8%E6%88%B7%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png">
<meta property="og:image" content="http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/%E5%AE%88%E6%8A%A4%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png">
<meta property="article:published_time" content="2022-09-21T07:12:22.000Z">
<meta property="article:modified_time" content="2022-10-21T09:10:20.035Z">
<meta property="article:author" content="chenshuli">
<meta property="article:tag" content="JUC">
<meta property="article:tag" content="并发编程">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/%E7%94%A8%E6%88%B7%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png">


<link rel="canonical" href="http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/","path":"2022/09/21/java/JUC并发编程/1_JUC概述/","title":"JUC概述"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JUC概述 | 陈树立的博客</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">陈树立的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E7%AE%80%E4%BB%8B"><span class="nav-number">1.</span> <span class="nav-text">1.1 简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E8%BF%9B%E7%A8%8B%E5%92%8C%E7%BA%BF%E7%A8%8B"><span class="nav-number">2.</span> <span class="nav-text">1.2 进程和线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%8A%B6%E6%80%81"><span class="nav-number">3.</span> <span class="nav-text">1.3 线程的状态</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-1-%E7%BA%BF%E7%A8%8B%E7%8A%B6%E6%80%81%E6%9E%9A%E4%B8%BE%E7%B1%BB"><span class="nav-number">3.1.</span> <span class="nav-text">1.3.1 线程状态枚举类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-2-wait-x2F-sleep%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">3.2.</span> <span class="nav-text">1.3.2 wait&#x2F;sleep的区别</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E5%B9%B6%E5%8F%91%E4%B8%8E%E5%B9%B6%E8%A1%8C"><span class="nav-number">4.</span> <span class="nav-text">1.4 并发与并行</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-1-%E4%B8%B2%E8%A1%8C%E6%A8%A1%E5%BC%8F"><span class="nav-number">4.1.</span> <span class="nav-text">1.4.1 串行模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-2-%E5%B9%B6%E8%A1%8C%E6%A8%A1%E5%BC%8F"><span class="nav-number">4.2.</span> <span class="nav-text">1.4.2 并行模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-3-%E5%B9%B6%E5%8F%91"><span class="nav-number">4.3.</span> <span class="nav-text">1.4.3 并发</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-%E7%AE%A1%E7%A8%8B"><span class="nav-number">5.</span> <span class="nav-text">1.5 管程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-6-%E7%94%A8%E6%88%B7%E7%BA%BF%E7%A8%8B%E5%92%8C%E5%AE%88%E6%8A%A4%E7%BA%BF%E7%A8%8B"><span class="nav-number">6.</span> <span class="nav-text">1.6 用户线程和守护线程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-7-%E7%BA%BF%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1"><span class="nav-number">7.</span> <span class="nav-text">1.7 线程间通信</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7-1-%E8%99%9A%E5%81%87%E5%94%A4%E9%86%92"><span class="nav-number">7.1.</span> <span class="nav-text">1.7.1 虚假唤醒</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7-2-%E5%AE%9A%E5%88%B6%E5%8C%96%E9%80%9A%E4%BF%A1"><span class="nav-number">7.2.</span> <span class="nav-text">1.7.2 定制化通信</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="chenshuli"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">chenshuli</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/chenshuli88" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chenshuli88" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/wangyicsl@126.com" title="E-Mail → wangyicsl@126.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="chenshuli">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="陈树立的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JUC概述 | 陈树立的博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JUC概述
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-09-21 15:12:22" itemprop="dateCreated datePublished" datetime="2022-09-21T15:12:22+08:00">2022-09-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-10-21 17:10:20" itemprop="dateModified" datetime="2022-10-21T17:10:20+08:00">2022-10-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
        </span>
    </span>

  
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JUC概述" href="/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::b87600dda57a4e1f45743a7a49cd689c" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.5k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="1-1-简介"><a href="#1-1-简介" class="headerlink" title="1.1 简介"></a>1.1 简介</h2><blockquote>
<p>JUC就是java.util.concurrent工具包的简称。这是一个处理线程的工具包，JDK1.5之后有的。</p>
</blockquote>
<h2 id="1-2-进程和线程"><a href="#1-2-进程和线程" class="headerlink" title="1.2 进程和线程"></a>1.2 进程和线程</h2><blockquote>
<p>进程：指在系统中正在运行的一个应用程序；资源分配的最小单位。<br>线程：系统分配处理器时间资源的基本单元，或者说进程之内独立执行的一个单元执行刘。程序执行的最小单位。</p>
</blockquote>
<h2 id="1-3-线程的状态"><a href="#1-3-线程的状态" class="headerlink" title="1.3 线程的状态"></a>1.3 线程的状态</h2><h3 id="1-3-1-线程状态枚举类"><a href="#1-3-1-线程状态枚举类" class="headerlink" title="1.3.1 线程状态枚举类"></a>1.3.1 线程状态枚举类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> <span class="title class_">ThreadState</span> &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 新建</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    NEW,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 准备就绪</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    RUNNABLE,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 阻塞</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    BLOCKED,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 等待（不见不散）</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    WAITING,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 超时等待（过时不候）</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    TIMED_WAITING,</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 终结</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    TERMINATED;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="1-3-2-wait-x2F-sleep的区别"><a href="#1-3-2-wait-x2F-sleep的区别" class="headerlink" title="1.3.2 wait&#x2F;sleep的区别"></a>1.3.2 wait&#x2F;sleep的区别</h3><ol>
<li><p>sleep是Thread的静态方法；wait是Object的方法，任务对象实例都能调用。</p>
</li>
<li><p>sleep不会释放锁，它也不需要占用锁。wait会释放锁，但调用它的前提是当前线程占有锁（即代码要在synchronized中）。</p>
</li>
<li><p>它们都可以被interrupted方法中断。</p>
</li>
</ol>
<h2 id="1-4-并发与并行"><a href="#1-4-并发与并行" class="headerlink" title="1.4 并发与并行"></a>1.4 并发与并行</h2><h3 id="1-4-1-串行模式"><a href="#1-4-1-串行模式" class="headerlink" title="1.4.1 串行模式"></a>1.4.1 串行模式</h3><blockquote>
<p>串行表示所有任务都一一按照先后顺序进行。串行一次只能取得一个任务，并执行这个任务。</p>
</blockquote>
<h3 id="1-4-2-并行模式"><a href="#1-4-2-并行模式" class="headerlink" title="1.4.2 并行模式"></a>1.4.2 并行模式</h3><blockquote>
<p>并行意味着可以同时取得多个任务，并同时去执行所取得的这些任务。</p>
</blockquote>
<h3 id="1-4-3-并发"><a href="#1-4-3-并发" class="headerlink" title="1.4.3 并发"></a>1.4.3 并发</h3><blockquote>
<p>并发指的是多个线程可以同时运行的现象。<br>并发：同一时刻多个线程再访问同一个资源，多个线程对一个点。例如春运抢票，电商秒杀。<br>并行：多项工作一起执行。例如泡方便面，电水壶烧水，一边撕调料包</p>
</blockquote>
<h2 id="1-5-管程"><a href="#1-5-管程" class="headerlink" title="1.5 管程"></a>1.5 管程</h2><blockquote>
<p>管程,操作系统中叫Monitor监视器，java中叫锁，是一种同步机制，保证同一个时间只有一个线程能访问被保护的资源。jvm同步是基于进入和退出，使用管程对象实现的。</p>
</blockquote>
<h2 id="1-6-用户线程和守护线程"><a href="#1-6-用户线程和守护线程" class="headerlink" title="1.6 用户线程和守护线程"></a>1.6 用户线程和守护线程</h2><blockquote>
<p>用户线程：自定义线程，平时用到的线程<br>守护线程：比如垃圾回收，一种特殊的后台线程</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//主线程结束了，用户线程还在运行，jvm存活</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="keyword">new</span> <span class="title class_">Thread</span>(() -&gt; &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">&quot;::&quot;</span> + Thread.currentThread().isDaemon());</span><br><span class="line">            <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;, <span class="string">&quot;aa&quot;</span>).start();</span><br><span class="line"></span><br><span class="line">        System.out.println(Thread.currentThread().getName() + <span class="string">&quot;  over&quot;</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>运行结果：<img src="/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/%E7%94%A8%E6%88%B7%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png" class="" title="用户线程运行结果"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//没有用户线程了，都是守护线程，主线程结束了，jvm结束</span></span><br><span class="line"><span class="type">Thread</span> <span class="variable">aa</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Thread</span>(() -&gt; &#123;</span><br><span class="line">            System.out.println(Thread.currentThread().getName() + <span class="string">&quot;::&quot;</span> + Thread.currentThread().isDaemon());</span><br><span class="line">            <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;, <span class="string">&quot;aa&quot;</span>);</span><br><span class="line">        <span class="comment">//设置守护线程</span></span><br><span class="line">        aa.setDaemon(<span class="literal">true</span>);</span><br><span class="line">        aa.start();</span><br><span class="line">        System.out.println(Thread.currentThread().getName() + <span class="string">&quot;  over&quot;</span>);</span><br></pre></td></tr></table></figure>

<p>运行结果：<img src="/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/1_JUC%E6%A6%82%E8%BF%B0/%E5%AE%88%E6%8A%A4%E7%BA%BF%E7%A8%8B%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png" class="" title="守护线程运行结果"></p>
<h2 id="1-7-线程间通信"><a href="#1-7-线程间通信" class="headerlink" title="1.7 线程间通信"></a>1.7 线程间通信</h2><h3 id="1-7-1-虚假唤醒"><a href="#1-7-1-虚假唤醒" class="headerlink" title="1.7.1 虚假唤醒"></a>1.7.1 虚假唤醒</h3><h3 id="1-7-2-定制化通信"><a href="#1-7-2-定制化通信" class="headerlink" title="1.7.2 定制化通信"></a>1.7.2 定制化通信</h3>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.jpg" alt="chenshuli 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.jpg" alt="chenshuli 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          <div class="post-tags">
              <a href="/tags/JUC/" rel="tag"># JUC</a>
              <a href="/tags/%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/" rel="tag"># 并发编程</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/09/06/%E4%BA%91%E5%8E%9F%E7%94%9F/docker/docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" rel="prev" title="docker常用命令">
                  <i class="fa fa-chevron-left"></i> docker常用命令
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/09/21/java/JUC%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B/2_Lock%E6%8E%A5%E5%8F%A3/" rel="next" title="Lock接口">
                  Lock接口 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="b87600dda57a4e1f45743a7a49cd689c"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">chenshuli</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">16k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">15 分钟</span>
  </span>
</div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cywgkfr9L","appkey":"045ab620a1fe071936bd2feddfbeaad8"}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
